#
#	Copyright 2010 Tommaso Previero
#	
#	This file is part of TeXdocGraphic.
#
#	TeXdocGraphic is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	TeXdocGraphic is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with TeXdocGraphic.  If not, see <http://www.gnu.org/licenses/>.
#	
#	Created:		26/09/2010
#

#the command used to compile the sources
JC = javac
#the flags to use for compile sources
FLAGJC = -d compiled/ -target 1.5 -source 5
#the command used to .jar the compiled files
JR = jar
#the flags to pass to the jar command
FLAGJR = cfm
#list of all the files in the sources directory
FILENAMES = $(shell find sources/ -type f -name "*.java")
#list of all the compiled files, modified for using as argument of the jar
COMPILED = $(shell cd compiled && find . -type f -name "*.class" | cut -c 3- | sed 's/^/-C compiled /' | sed 's/\$$/\\\$$/' && cd ..)
#the number of the current revision preceed from a 'R' (using tmp file, avoiding the 'svn: Write error: Broken pipe')
NOREV = R$(shell svn info > svnInfoTmp && grep -F -m 1 "Rev" svnInfoTmp | cut -d: -f2 | cut -c 2- && rm svnInfoTmp)
#the number of the current revision (using tmp file, avoiding the 'svn: Write error: Broken pipe')
NUMREV = $(shell svn info > svnInfoTmp && grep -F -m 1 "Rev" svnInfoTmp | cut -d: -f2 | cut -c 2- && rm svnInfoTmp)

default:clean compile jar

all:clean compile jar deb app

install: jar
	@echo -n Creating program folder...
	@mkdir /usr/local/tg
	@echo done.
	
	@echo -n Copying files...
	
	@cp dist/TeXdocGraphic$(NOREV).jar /usr/local/tg/
	
	@cp res/tdg /usr/local/bin/
	@sed -i -e "s/TeXdocGraphic.*jar/TeXdocGraphic$(NOREV).jar/g" /usr/local/bin/tdg
	
	@cp res/texdocgraphic.desktop /usr/share/applications/
	
	@cp res/images/icon32.png /usr/share/icons/
	
	@echo done.

uninstall:
	@echo -n Removing the application...
	
	@rm -rf /usr/local/tg
	@rm -rf /usr/local/bin/tdg
	@rm -rf /usr/share/applications/texdocgraphic.desktop
	@rm -rf /usr/share/incons/icon32.png
	
	@echo done.

doc: compile
	@echo Building new documentation...
	@javadoc -private -sourcepath sources/ -classpath compiled/ -d doc/ $(FILENAMES)
	@echo done.

deb: jar
	@echo -n Creating the structure of the .deb...
	
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/DEBIAN
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr/local
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr/local/bin
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr/local/tg
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr/share
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr/share/applications
	@mkdir dist/TeXdocGraphic-0.$(NUMREV)/usr/share/icons
	
	@echo done.
	
	@echo -n Importing files...
	
	@cp res/control dist/TeXdocGraphic-0.$(NUMREV)/DEBIAN
	@sed -i -e "s/00/$(NUMREV)/g" dist/TeXdocGraphic-0.$(NUMREV)/DEBIAN/control
	@cp dist/TeXdocGraphic$(NOREV).jar dist/TeXdocGraphic-0.$(NUMREV)/usr/local/tg
	@cp res/tdg dist/TeXdocGraphic-0.$(NUMREV)/usr/local/bin
	@sed -i -e "s/TeXdocGraphic.*jar/TeXdocGraphic$(NOREV).jar/g" dist/TeXdocGraphic-0.$(NUMREV)/usr/local/bin/tdg
	@cp res/texdocgraphic.desktop dist/TeXdocGraphic-0.$(NUMREV)/usr/share/applications/
	@cp res/images/icon32.png dist/TeXdocGraphic-0.$(NUMREV)/usr/share/icons
	
	@echo done.
	
	@echo building TeXdocGraphic-0.$(NUMREV).deb...
	@dpkg-deb --build dist/TeXdocGraphic-0.$(NUMREV)
	@echo done.

app: jar
	@echo -n Creating the dist/TeXdocGraphic$(NOREV).app...
	
	@mkdir dist/TeXdocGraphic$(NOREV).app
	@mkdir dist/TeXdocGraphic$(NOREV).app/Contents
	@mkdir dist/TeXdocGraphic$(NOREV).app/Contents/MacOS
	@mkdir dist/TeXdocGraphic$(NOREV).app/Contents/Resources
	@mkdir dist/TeXdocGraphic$(NOREV).app/Contents/Resources/Java
	
	@cp res/Mac/PkgInfo dist/TeXdocGraphic$(NOREV).app/Contents/
	@cp res/Mac/Info.plist dist/TeXdocGraphic$(NOREV).app/Contents/
	@cp res/images/icon.icns dist/TeXdocGraphic${NOREV}.app/Contents/Resources/
	@cp res/Mac/JavaApplicationStub dist/TeXdocGraphic$(NOREV).app/Contents/MacOS/
	@cp dist/TeXdocGraphic$(NOREV).jar dist/TeXdocGraphic$(NOREV).app/Contents/Resources/Java/
	
	@sed -i -e "s/TeXdocGraphic.*jar/TeXdocGraphic$(NOREV).jar/g" dist/TeXdocGraphic$(NOREV).app/Contents/Info.plist
	
	@echo done.

compile: clean
	@echo -n Compiling "sources/*.java" in "compiled/*.class"...
	
	@$(JC) $(FLAGJC) $(FILENAMES)
	
	@echo done.

jar: compile
	@echo -n Creating the jar and set its privileges...
	
	@mkdir compiled/it/texdocgraphic/gui/images
	@cp res/images/splash.png compiled/it/texdocgraphic/gui/images/splash.png
	@cp res/images/icon32.png compiled/it/texdocgraphic/gui/images/icon32.png
	
	@$(JR) $(FLAGJR) dist/TeXdocGraphic$(NOREV).jar res/MANIFEST.MF $(COMPILED) -C compiled it/texdocgraphic/gui/images/splash.png -C compiled it/texdocgraphic/gui/images/icon32.png COPYING
	
	@chmod 755 dist/TeXdocGraphic$(NOREV).jar
	
	@echo done.

clean:
	@echo -n Removing old compiled, distributions and documentation files...
	
	@rm -rf compiled/*
	@rm -rf dist/*
	@rm -rf doc/*
	
	@echo done.
